﻿using System;
using System.Web.UI.WebControls;
using Wicresoft.RDW.Presentation;
using Wicresoft.RDW.Model;
using Wicresoft.RDW.Utility;

namespace Wicresoft.RDW.Content
{
    public partial class CustomerType : BasePage_BackEnd
    {
        protected void GvCustType_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Model.VisitType visitType = e.Row.DataItem as Model.VisitType;
                if (visitType != null)
                {
                    int i = 0;
                    e.Row.Cells[i++].Text = visitType.Name_CN;
                    e.Row.Cells[i++].Text = visitType.Name_EN;
                }
            }
        }

        protected void gvCustRevamp1_Updating(object sender, GridViewUpdateEventArgs e)
        {
            int index = e.RowIndex;
            txtName_CN.Text = gvCustType1.Rows[index].Cells[0].Text;
            txtName_EN.Text = gvCustType1.Rows[index].Cells[1].Text;
            string Id = gvCustType1.DataKeys[index].Value.ToString();
            ddlType.SelectedIndex = VisitTypePresent.GetById(int.Parse(Id)).ParentId;
            lblId.Text = Id;
            phType.Visible = false;
            lbtnStatus(true);
            SetControlStatus(ControStatus.ReworkShow);
        }

        //protected void lbtnDelete1_Deleting(object sender, GridViewDeleteEventArgs e)
        //{
        //    int index = e.RowIndex;
        //    string Id = gvCustType1.DataKeys[index].Value.ToString();
        //    VisitTypePresent.Delete(int.Parse(Id));
        //    GetDataSource();
        //}

        protected void gvCustRevamp2_Updating(object sender, GridViewUpdateEventArgs e)
        {
            int index = e.RowIndex;
            txtName_CN.Text = gvCustType2.Rows[index].Cells[0].Text;
            txtName_EN.Text = gvCustType2.Rows[index].Cells[1].Text;
            string Id = gvCustType2.DataKeys[index].Value.ToString();
            ddlType.SelectedIndex = VisitTypePresent.GetById(int.Parse(Id)).ParentId;
            lblId.Text = Id;
            phType.Visible = true;
            lbtnStatus(true);
            SetControlStatus(ControStatus.ReworkShow);
        }

        protected void gvCustDelete2_Deleting(object sender, GridViewDeleteEventArgs e)
        {
            int index = e.RowIndex;
            string Id = gvCustType2.DataKeys[index].Value.ToString();
            VisitTypePresent.Delete(int.Parse(Id));
            GetDataSource();
        }

        protected void btnShowOnClick(object sender, EventArgs e)
        {
            txtName_CN.Text = string.Empty;
            txtName_EN.Text = string.Empty;
            ddlType.SelectedIndex = 0;
            lblId.Text = string.Empty;

            phType.Visible = true;
            SetControlStatus(ControStatus.AddShow);
            lbtnStatus(false);
        }

        protected void btnAddOnClick(object sender, EventArgs e)
        {
            VisitType visitType = new VisitType();
            visitType.Name_CN = txtName_CN.Text;
            visitType.Name_EN = txtName_EN.Text;
            visitType.ParentId = ddlType.SelectedIndex;
            visitType.Level = 2;
            VisitTypePresent.Create(visitType);

            SetControlStatus(ControStatus.Add);
            GetDataSource();
        }

        protected void btnRevampOnClick(object sender, EventArgs e)
        {
            VisitType visitType = VisitTypePresent.GetById(int.Parse(lblId.Text));
            visitType.Name_CN = txtName_CN.Text;
            visitType.Name_EN = txtName_EN.Text;
            visitType.ParentId = ddlType.SelectedIndex;
            VisitTypePresent.Update(visitType);

            SetControlStatus(ControStatus.Rework);
            lbtnStatus(false);
            GetDataSource();

        }

        protected void btnCancelOnClick(object sender, EventArgs e)
        {
            PlaceHolder.Visible = false;
            lbtnStatus(false);
        }

        private void SetControlStatus(ControStatus conSta)
        {
            switch (conSta)
            {
                case ControStatus.Add:
                    txtName_CN.Text = string.Empty;
                    txtName_EN.Text = string.Empty;
                    ddlType.SelectedIndex = 0;
                    break;
                case ControStatus.Rework:
                    PlaceHolder.Visible = false;
                    lblId.Text = string.Empty;
                    break;
                case ControStatus.Delete:
                    break;
                case ControStatus.ReworkShow:
                    btnAdd.Visible = false;
                    btnRevamp.Visible = true;
                    PlaceHolder.Visible = true;
                    break;
                case ControStatus.AddShow:
                    btnAdd.Visible = true;
                    btnRevamp.Visible = false;
                    PlaceHolder.Visible = true;
                    break;
            }

        }

        protected void lbtnStatus(bool status)
        {
            int count1 = gvCustType1.Rows.Count;
            int count2 = gvCustType2.Rows.Count;
            if (status)
            {
                for (int i = 0; i < count2; i++)
                {
                    LinkButton lbtnDelete2 = gvCustType2.Rows[i].FindControl("lbtnDelete2") as LinkButton;
                    lbtnDelete2.Attributes.Add("disabled", "disabled");
                    lbtnDelete2.Attributes.Remove("onclick");
                    lbtnDelete2.Attributes.Add("onclick", "return false;");
                }
            }
            else
            {
                for (int i = 0; i < count2; i++)
                {
                    LinkButton lbtnDelete2 = gvCustType2.Rows[i].FindControl("lbtnDelete2") as LinkButton;
                    lbtnDelete2.Attributes.Remove("disabled");
                    lbtnDelete2.Attributes.Remove("onclick");
                    lbtnDelete2.Attributes.Add("onclick", "return confirm('" + Resources.NameResource.Delete_Confirm + "？');");
                }
            }
        }


        protected void GetDataSource()
        {
            var customerType1 = VisitTypePresent.GetList(g => g.Level == 1);
            gvCustType1.DataSource = customerType1;
            gvCustType1.DataBind();

            var customerType2 = VisitTypePresent.GetList(g => g.Level == 2);
            gvCustType2.DataSource = customerType2;
            gvCustType2.DataBind();
            lbtnStatus(false);
        }

        public override void InitControlResource()
        {
            lblNameCN.Text = Resources.NameResource.Customer_TypeName_CN;
            lblNameEN.Text = Resources.NameResource.Customer_TypeName_EN;
            lblManage.Text = Resources.ManageResource.mgr_lblAction;

            btnShow.Text = Resources.ManageResource.mgr_btnAddCustomerType;
            btnAdd.Text = Resources.ManageResource.mgr_btnOK;
            btnRevamp.Text = Resources.ManageResource.mgr_btnOK;
            btnCancel.Text = Resources.ManageResource.mgr_btnCancel;

            rfvName_CN.Text = Resources.CommonResource.NotNull;
            rfvName_EN.Text = Resources.CommonResource.NotNull;
            lblType.Text = Resources.NameResource.Customer_Type;

            GetDataSource();
        }

    }
}